package com.chenjiagou.ordertickets.service.impl;

import com.chenjiagou.ordertickets.entity.Flight;
import com.chenjiagou.ordertickets.dao.FlightDao;
import com.chenjiagou.ordertickets.pojo.PageResult;
import com.chenjiagou.ordertickets.service.FlightService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Flight)表服务实现类
 *
 * @author makejava
 * @since 2025-06-24 08:54:27
 */
@Service("flightService")
public class FlightServiceImpl implements FlightService {
    @Resource
    private FlightDao flightDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Flight queryById(Long id) {
        return this.flightDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param flight 实例对象
     * @return 实例对象
     */
    @Override
    public Flight insert(Flight flight) {
        this.flightDao.insert(flight);
        return flight;
    }

    /**
     * 修改数据
     *
     * @param flight 实例对象
     * @return 实例对象
     */
    @Override
    public Flight update(Flight flight) {
        this.flightDao.update(flight);
        return this.queryById(flight.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.flightDao.deleteById(id) > 0;
    }
    /**
     * 查询航班
     * @return
     */
    @Override
    public PageResult<Flight> searchFlights(String depCity, String arrCity, Date depDate,int page, int size) {
        int offset = (page - 1) * size;
        List<Flight> flights = flightDao.searchFlights(depCity, arrCity, depDate, offset, size);
        int total = flightDao.countFlights(depCity, arrCity, depDate);
        int totalPages = (int) Math.ceil((double) total / size);
        return new PageResult<>(flights, totalPages);
    }
}
